/*
 * Created on 12. des.. 2006
 * Created by Tomas
 */
package gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeSelectionModel;

import placing.Place;
import placing.Room;

/**
 * 
 * A panel that presents the roomModel as a tree.
 * 
 * @author Tomas Ekeli
 *
 */
public class RoomOverviewPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private Presenter parent;
	
	private TreeModel model;
	private JTree tree;

	public RoomOverviewPanel(Presenter p) {
		super(new GridLayout(1, 0));
		
		parent = p;
		model = new RoomTreeModel(parent);
		
		//create the tree
		tree = new JTree(model);
		
		//Only allow single selections in the tree
		tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		DefaultTreeCellRenderer renderer = new RoomTreeCellRenderer();
		tree.setCellRenderer(renderer);
		
		//acitvate tool tips
		ToolTipManager.sharedInstance().registerComponent(tree);
		
		//add the tree to a scroll pane
		JScrollPane scrollPane = new JScrollPane(tree);
		scrollPane.setPreferredSize(new Dimension(200, 500));
		
		//add the scroll pane to this panel
		add(scrollPane);
	}

	public JTree getTree() {
		return tree;
	}


/**
 * 
 * The RoomTreeCellRenderer makes sure the Rooms and Places get their 
 * appropriate icons and tooltips in the GUI.
 * 
 * @author Tomas
 *
 */
public class RoomTreeCellRenderer extends DefaultTreeCellRenderer {
	private static final long serialVersionUID = 1L;
	private Icon rootIcon;
	private Icon placeAvailableIcon, placeReservedIcon, placeTakenIcon;
	private Icon roomClosedAvailableIcon, roomClosedEmptyIcon, roomClosedFullIcon;
	private Icon roomOpenAvailableIcon, roomOpenEmptyIcon, roomOpenFullIcon;
	
	/*
	 * The path to the icon directory, and their names. The path is relative
	 * to this class.
	 */
	private String path = "images/";
	private String rootP				= "root.png";
	private String placeAvailableP 		= "p-available.png";
	private String placeReservedP 		= "p-reserved.png";
	private String placeTakenP			= "p-taken.png";
	private String roomClosedAvailableP = "r-closed-available.png";
	private String roomClosedEmptyP 	= "r-closed-empty.png";
	private String roomClosedFullP 		= "r-closed-full.png";
	private String roomOpenAvailableP 	= "r-open-available.png";
	private String roomOpenEmptyP 		= "r-open-empty.png";
	private String roomOpenFullP 		= "r-open-full.png";
	
	/*
	 * The tool tips for the places and rooms
	 */
	private String rootTT 			= "The rooms and places";
	private String roomAvailableTT 	= "Room has available places";
	private String roomEmptyTT		= "Room has no places";
	private String roomFullTT		= "Full room";
	private String placeAvailableTT = "Free place";
	private String placeReservedTT	= "Reserved place";
	private String placeTakenTT		= "Occupied place";
	
	
	public RoomTreeCellRenderer() {
		super();
		
		//The icons
		rootIcon				= getIcon(path+rootP);
		
		placeAvailableIcon 		= getIcon(path+placeAvailableP);
		placeReservedIcon 		= getIcon(path+placeReservedP);
		placeTakenIcon 			= getIcon(path+placeTakenP);
		
		roomClosedAvailableIcon = getIcon(path+roomClosedAvailableP);
		roomClosedEmptyIcon		= getIcon(path+roomClosedEmptyP);
		roomClosedFullIcon		= getIcon(path+roomClosedFullP);
		
		roomOpenAvailableIcon 	= getIcon(path+roomOpenAvailableP);
		roomOpenEmptyIcon		= getIcon(path+roomOpenEmptyP);
		roomOpenFullIcon		= getIcon(path+roomOpenFullP);
	}

	@Override
	public Component getTreeCellRendererComponent(	JTree tree,
													Object value,
													boolean sel, 
													boolean expanded, 
													boolean leaf, 
													int row, 
													boolean hasFocus) {
		super.getTreeCellRendererComponent(tree, value, sel, expanded,
				leaf, row, hasFocus);
		
		if (leaf) { //empty room or a place
			try {
				((Room)value).getName();
				setIcon(roomClosedEmptyIcon);
				setToolTipText(roomEmptyTT);
			} catch (ClassCastException e) {
				/*
				 * it is a place - set the icon indicating its status
				 * (free, reserved or taken)
				 */
				try {
					Place p = (Place)value;
					if (p.isFree()) { 
						if (p.isReserved()) {
							setIcon(placeReservedIcon);
							setToolTipText(placeReservedTT);
						} else {
							setIcon(placeAvailableIcon);
							setToolTipText(placeAvailableTT);
						}
					} else {
						setIcon(placeTakenIcon);
						setToolTipText(placeTakenTT);
					}
				} catch (ClassCastException e1) {
					//it's not a Place, must be the Root
					setIcon(rootIcon);
					setToolTipText(rootTT);
				}
			}
			
		} else { //root or a non-empty room
			try {
				Room r = (Room)value;
				if (expanded) { //the open rooms
					if (r.getNumPlaces() == 0) {
						setIcon(roomOpenEmptyIcon);
						setToolTipText(roomEmptyTT);
						
					} else {
						if (r.getNumFreePlaces() == 0) {
							setIcon(roomOpenFullIcon);
							setToolTipText(roomFullTT);
						} else {
							setIcon(roomOpenAvailableIcon);
							setToolTipText(roomAvailableTT);
						}
					} 
					
				} else { //the closed rooms - no empty ones (that's a leaf)
					if (r.getNumFreePlaces() == 0) {
						setIcon(roomClosedFullIcon);
						setToolTipText(roomFullTT);
					} else {
						setIcon(roomClosedAvailableIcon);
						setToolTipText(roomAvailableTT);
					}
				}
			} catch (ClassCastException e) {
				// it's not a room, must be the Root
				setIcon(rootIcon);
				setToolTipText(rootTT);
			}
			
		}
		
		
		return this;
	}
	
	private Icon getIcon(String path) {
		URL imageFileURL = RoomOverviewPanel.class.getResource(path);
		ImageIcon icon;
		try {
			icon = new ImageIcon(imageFileURL);
		} catch (NullPointerException e) { //the icon could not be found
			System.err.println("Could not find icon: " + path);
			icon = null;
		}
		return icon;
	}
}
}
